package jp.co.tms.internal.runserver.protocol;

import java.util.Arrays;

import jp.co.tms.internal.util.NumberUtil;

public abstract class Package implements IPackage{
	
	public static final int HEADER_VALUE = 0x00 ;
	public static final int TAIL_VALUE   = 0xFF ;
	
	private int _type;
	
	private byte[] _buffer = new byte[PACKAGE_LEN] ; 
	
	public Package( int type ){
		this._type = type ;
	}
	
	public byte[] getBytes( byte[] data ){
		
		Arrays.fill( _buffer, (byte)0 ) ;
		_buffer[0] = HEADER_VALUE ;
		
		byte[] values = NumberUtil.intToByteArray( _type ) ;
		System.arraycopy( values, 
				0, 
				_buffer, 
				1, 
				values.length
				);
		
		if ( null != data ){
			values = NumberUtil.intToByteArray( data.length );
			System.arraycopy( values, 
					0, 
					_buffer, 
					1 + SYS_INT_SIZE, 
					values.length 
					) ;
			
			if ( data != null && data.length != 0 ){
				System.arraycopy( data, 
						0, 
						_buffer, 
						1 + 2 * SYS_INT_SIZE, 
						data.length 
						) ;	
			}
		}
		
		_buffer[PACKAGE_LEN - 1] = (byte)TAIL_VALUE ;
		
		return _buffer ;
	}
}
